package com.five.dao.impl.backstage;

import com.five.dao.backstage.CountDao;
import com.five.entity.backstage.LayDate;
import com.five.util.DBUtil;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Repository
public class CountDaoImpl implements CountDao {
    @Override
    public Map<Object, Object> selectPrice(LayDate layDate) {
        int minMonth=1;
        int maxMonth=12;
        //订房总钱
        String sql="select month(createtime)month,sum(price)price from t_order where 1=1 ";
        if(layDate.getYear() != null ){
            sql+=" and year(createtime)="+layDate.getYear();
        }else{
            sql+=" and year(createtime) =2020";
        }

        if(layDate.getMonthMin() != null ){
            sql+=" and month(createtime) >="+layDate.getMonthMin();
            minMonth= Integer.parseInt(layDate.getMonthMin());
        }else{
            sql+=" and month(createtime)>=1";
        }

        if(layDate.getMonthMax() != null ){
            sql+=" and month(createtime) <="+layDate.getMonthMax();
            maxMonth= Integer.parseInt(layDate.getMonthMax());
        }else{
            sql+=" and month(createtime)<=12";
        }
        sql+=" group by month(createtime)";


        //退房金钱
        String sql2="select month(tf.outdate)month,sum(tf.tsmoney)price from \n" +
                "(select distinct orderid,outdate,tsmoney from t_tuifang)tf where 1=1 ";
        if(layDate.getYear() != null ){
            sql2+=" and year(outdate)="+layDate.getYear();
        }else{
            sql2+=" and year(outdate) =2020";
        }

        if(layDate.getMonthMin() != null ){
            sql2+=" and month(outdate) >="+layDate.getMonthMin();
            minMonth= Integer.parseInt(layDate.getMonthMin());
        }else{
            sql2+=" and month(outdate)>=1";
        }

        if(layDate.getMonthMax() != null ){
            sql2+=" and month(outdate) <="+layDate.getMonthMax();
            maxMonth= Integer.parseInt(layDate.getMonthMax());
        }else{
            sql2+=" and month(outdate)<=12";
        }
        sql2+=" group by month";


        //续房总金额
        String sql3="select month(begintime)month,sum(xfprice)price from t_xufang where 1=1 ";
        if(layDate.getYear() != null ){
            sql3+=" and year(begintime)="+layDate.getYear();
        }else{
            sql3+=" and year(begintime) =2020";
        }

        if(layDate.getMonthMin() != null ){
            sql3+=" and month(begintime) >="+layDate.getMonthMin();
            minMonth= Integer.parseInt(layDate.getMonthMin());
        }else{
            sql3+=" and month(begintime)>=1";
        }

        if(layDate.getMonthMax() != null ){
            sql3+=" and month(begintime) <="+layDate.getMonthMax();
            maxMonth= Integer.parseInt(layDate.getMonthMax());
        }else{
            sql3+=" and month(begintime)<=12";
        }
        sql3+=" group by month(begintime)";
        List<Map> maps = DBUtil.queryMap(sql);//总金额
        List<Map> maps1 = DBUtil.queryMap(sql2);
        List<Map> maps2 = DBUtil.queryMap(sql3);
        /*System.out.println(maps);
        System.out.println("========================");
        System.out.println(maps1);
        System.out.println("=========================");
        System.out.println(maps2);*/
        //总钱数 = 总金额-退房金额+续房金额
        int a=1;
        for (Map map : maps) {
            //总钱
            String s = map.get("price") + "";
            double zprice = Double.parseDouble(s);
            //月份
            String s1 = map.get("month") + "";
            int zmonth = Integer.parseInt(s1);//月份
            for (Map map1 : maps1) {
                //退房钱
                String s2 = map1.get("price") + "";
                double tprice = Double.parseDouble(s2);
                //月份
                String s3 = map1.get("month") + "";
                int tmonth = Integer.parseInt(s3);
                if (zmonth==tmonth){
                    zprice-=tprice;
                    map.replace("price",zprice);
                }
                for (Map map2 : maps2) {
                    //续房总金额
                    String s4 = map2.get("price") + "";
                    double xprice = Double.parseDouble(s4);
                    //月份
                    String s5 = map2.get("month") + "";
                    int xmonth = Integer.parseInt(s5);
                    if (zmonth==xmonth){

                        zprice+=xprice;
                        map.replace("price",zprice);
                    }
                }
            }
        }
        //System.out.println(a);
        /*System.out.println("+++++++++++++++++++++++++");
        System.out.println(maps);*/
        /*int minMonth=1;
        int maxMonth=12;
        String sql="select month(createtime),sum(price)price from t_order where 1=1 ";
        if(layDate.getYear() != null ){
            sql+=" and year(createtime)="+layDate.getYear();
        }else{
            sql+=" and year(createtime) =2020";
        }

        if(layDate.getMonthMin() != null ){
            sql+=" and month(createtime) >="+layDate.getMonthMin();
            minMonth= Integer.parseInt(layDate.getMonthMin());
        }else{
            sql+=" and month(createtime)>=1";
        }

        if(layDate.getMonthMax() != null ){
            sql+=" and month(createtime) <="+layDate.getMonthMax();
            maxMonth= Integer.parseInt(layDate.getMonthMax());
        }else{
            sql+=" and month(createtime)<=12";
        }
        sql+=" group by month(createtime)";*/
        //月营业额

        Map<Object, Object> mp = new HashMap<>();
        List<Object> monthData = new ArrayList<>();

        List<Double> rukuMoney=new ArrayList<>();
        for (int i = minMonth; i <=maxMonth ; i++) {
            double value=0;
            monthData.add(i+"");
            for (Map map : maps) {
                if(Integer.parseInt(map.get("month")+"")==i){
                    value=Double.parseDouble(map.get("price")+"");
                }
            }
            rukuMoney.add(value);
        }

        mp.put("monthData",monthData);
        mp.put("rukuMoney",rukuMoney);

        return mp;
    }

    @Override
    public List<Map> selectAll() {
        /*String  sql="select t.name name,sum(price) value from t_order o,t_type t where t.id=o.roomtype GROUP BY o.roomtype ";*/
        //总金额
        String sql1="select tt.name name,sum(td.price)value from t_order td join t_type tt on\n" +
                " td.roomtype=tt.id GROUP BY td.roomtype";
        //退房
        String sql2="select tt.name name,sum(tf.tsmoney)value from (\n" +
                "\tselect  distinct orderid,tsmoney  from t_tuifang \n" +
                ")tf join t_order td on tf.orderid=td.orderid join \n" +
                "t_type tt on td.roomtype=tt.id GROUP BY td.roomtype";
        //续房
        String sql3="select tp.name name,sum(tx.xfprice)value from t_xufang tx \n" +
                "join t_tuifang tt on tx.ruzhuid=tt.ruzhuid \n" +
                "join t_order td on tt.orderid=td.orderid \n" +
                "join t_type tp on td.roomtype=tp.id GROUP BY td.roomtype";
        List<Map> maps1 = DBUtil.queryMap(sql1);//总金额
        List<Map> maps2 = DBUtil.queryMap(sql2);//退房钱
        List<Map> maps3 = DBUtil.queryMap(sql3);//续房钱
        /*System.out.println(maps1);
        System.out.println("========================");
        System.out.println(maps2);
        System.out.println("=========================");
        System.out.println(maps3);*/
        for (Map map1 : maps1) {
            String zname = map1.get("name") + "";
            String s = map1.get("value") + "";
            double zprice = Double.parseDouble(s);
            for (Map map2 : maps2) {
                String tname = map2.get("name") + "";
                String s1 = map2.get("value") + "";
                double tprice = Double.parseDouble(s1);
                if (tname.equals(zname)){
                    zprice-=tprice;
                    map1.replace("value",zprice);
                }
                for (Map map3 : maps3) {
                    String xname = map3.get("name") + "";
                    String s2 = map3.get("value") + "";
                    double xprice = Double.parseDouble(s2);
                    if (xname.equals(zname)){
                        zprice+=xprice;
                        map1.replace("value",zprice);
                    }
                }
            }

        }
        //System.out.println(maps1);





        //System.out.println(DBUtil.queryMap(sql));
        return maps1;
    }

    @Override
    public List<Map> selectKu() {
        String sql="select 'name' name, counts value from t_facility where typeid=2";
        return DBUtil.queryMap(sql);
    }
}
